package game;

import java.util.Random;

import javax.swing.JOptionPane;

public class RandomEvent {
    private Round round;
    private Random r;
    private int m;
    private int roundNum;

    public RandomEvent(Game currentGame, Round round) {
	this.round = round;
	this.r = new Random();

	this.roundNum = round.getRound();
	switch (roundNum) {
	case 1:
	case 2:
	case 3:
	    m = 25;
	    break;
	case 4:
	case 5:
	case 6:
	case 7:
	    m = 50;
	    break;
	case 8:
	case 9:
	case 10:
	case 11:
	    m = 75;
	    break;
	case 12:
	    m = 100;
	    break;
	default:
	    break;
	}

	int eventChance = r.nextInt(100);
	if (eventChance <= 27) {
	    int event = r.nextInt(6);
	    if (!isBad(event)
		    || round.getCurrPlayer() != currentGame.getSortedPlayer(0))
		this.perform(event);
	}
    }

    private void perform(int event) {
	switch (event) {
	case 0:
	    round.getCurrPlayerResourceBasket().addResource(0, 3);
	    round.getCurrPlayerResourceBasket().addResource(1, 2);
	    JOptionPane.showMessageDialog(null, "YOU JUST RECEIVED A PACKAGE "
		    + "FROM THE GT ALUMNI CONTAINING 3 FOOD AND 2 ENERGY UNITS");
	    break;
	case 1:
	    round.getCurrPlayerResourceBasket().addResource(2, 2);
	    JOptionPane.showMessageDialog(null, "A WANDERING TECH STUDENT "
		    + "REPAID YOUR HOSPITALITY BY LEAVING TWO BARS OF ORE");
	    break;
	case 2:
	    round.getCurrPlayerResourceBasket().addResource(4, 8 * m);
	    JOptionPane.showMessageDialog(null, "THE MUSEUM BOUGHT YOUR "
		    + "ANTIQUE PERSONAL COMPUTER FOR $" + 8 * m);
	    break;
	case 3:
	    round.getCurrPlayerResourceBasket().addResource(4, 2 * m);
	    JOptionPane.showMessageDialog(null, "YOU FOUND A DEAD MOOSE RAT "
		    + "AND SOLD THE HIDE FOR $" + 2 * m);
	    break;
	case 4:
	    if (round.getCurrPlayerResource(4) - 4 * m < 0)
		round.getCurrPlayerResourceBasket().setResource(4, 0);
	    else
		round.getCurrPlayerResourceBasket().subtractResource(4, 4 * m);
	    JOptionPane.showMessageDialog(null, "FLYING CAT-BUGS ATE THE ROOF"
		    + " OFF YOUR HOUSE. REPAIRS COST $" + 4 * m);
	    break;
//	case 5:
//	    if (round.getCurrPlayerResource(2) >= 2) {
//		int newAmt = round.getCurrPlayerResource(2) / 2;
//		round.getCurrPlayerResourceBasket().setResource(2, newAmt);
//	    }
//	    JOptionPane.showMessageDialog(null, "MISCHIEVOUS UGA STUDENTS BROKE"
//		    + " INTO YOUR STORAGE SHED AND STOLE HALF YOUR FOOD");
//	    break;
	case 5:
	    if (round.getCurrPlayerResource(4) - 6 * m < 0)
		round.getCurrPlayerResourceBasket().setResource(4, 0);
	    else
		round.getCurrPlayerResourceBasket().subtractResource(4, 6 * m);
	    JOptionPane.showMessageDialog(null,"YOUR SPACE GYPSY INLAWS MADE "
		    + "A MESS OF THE TOWN. IT COST YOU $" + 6 * m
		    + " TO CLEAN IT UP");
	    break;
	default:
	    break;
	}
    }

    private boolean isBad(int num) {
	switch (num) {
	case 0:
	case 1:
	case 2:
	case 3:
	    return true;
	default:
	    return false;
	}
    }
}
